﻿using famails.bus;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails
{
    public partial class autodirection : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string param = Request.QueryString["enc"];
            if (param != null)
            {
                lblResult.Text = Server.UrlDecode(param);
                param = FaMailUtils.QueryStringModule.Decrypt(Server.UrlDecode(param).Replace(" ","+"));

                lblResult.Text = Server.UrlDecode(param);
                
                
                DataTable tbResult = new UserLoginBUS().GetByUserByEmail(param.Substring(4));
                if (tbResult.Rows.Count > 0)
                {
                    if (Convert.ToBoolean(tbResult.Rows[0]["is_block"]))
                    {
                        Response.Redirect("/backend/page/login.aspx", false);
                        return;
                    }
                    UserLoginDTO userLogin = new UserLoginDTO();
                    userLogin.UserId = int.Parse(tbResult.Rows[0]["UserId"].ToString());
                    userLogin.Username = tbResult.Rows[0]["Username"].ToString();
                    userLogin.Password = tbResult.Rows[0]["Password"].ToString();
                    userLogin.DepartmentId = int.Parse(tbResult.Rows[0]["DepartmentId"].ToString());
                    userLogin.UserType = int.Parse(tbResult.Rows[0]["UserType"].ToString());
                    userLogin.ClientID = int.Parse(tbResult.Rows[0]["ClientID"].ToString());

                    // Tạo session user login
                    Session["us-login"] = userLogin;
                    Session["UserName"] = userLogin.Username;
                    Session["UserId"] = userLogin.UserId;
                }
                else { Response.Redirect("/backend/page/login.aspx", false); }
               if (param.Substring(0,4) == "auto")
                   Response.Redirect("/backend/page/create-event.aspx", false);
               else if (param.Substring(0, 4) == "broa")
                   Response.Redirect("/backend/page/send-register.aspx", false);
               else if (param.Substring(0, 4) == "disc")
                   Response.Redirect("/backend/customer/Discount.aspx", false);
               else if (param.Substring(0, 4) == "clie")
                   Response.Redirect("/backend/customer/Customers.aspx", false);
               else if (param.Substring(0,4) == "cust")
                   Response.Redirect("/backend/page/Email.aspx", false);
               else if (param.Substring(0, 4) == "dash")
                   Response.Redirect("/backend/page/senddashboard.aspx", false);
            }
        }
    }
}